<template>
  <div>
    <div class="order">
      <div v-if="userOrder.length === 0" class="empty">当前订单为空哦~~~~</div>
      <div class="order-items" v-for="order in userOrder" :key="order.id">
        <div class="order-title">商品名：{{ order.name }}</div>
        <div class="order-goods-list">
          <div class="order-goods">
            <img :src="order.imgUrl" alt="" class="order-goods-img" />
            <p class="order-goods-intro">{{ order.title }}</p>
            <p class="order-goods-price">￥&nbsp;{{ order.price }}&nbsp;元</p>
            <div class="order-goods-num">数量：{{ order.ordernum || order.num }}</div>
          </div>
        </div>
        <div class="order-total">总价：￥{{ order.ordernum * order.price }}</div>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      userOrder: [],
    }
  },
  created() {
    // 判断用户信息
    const userInfo = JSON.parse(localStorage.getItem('userInfo') || '{}')
    if (!userInfo.username) {
      this.$router.push('/login')
      return
    }
    const username = userInfo.username

    fetch('/data/orderlist.json')
      .then((res) => res.json())
      .then((data) => {
        // 只取当前用户的订单（每个订单对象有username和goods）
        this.userOrder = data
          .filter((item) => item.username === username)
          .map((item) => item.goods)
          .flat()
      })
      .catch(() => {
        this.userOrder = []
      })
  },
}
</script>

<style scoped>
* {
  margin: 0;
  padding: 0;
}
.order {
  width: 1110px;
  min-height: 700px;
  margin: 30px auto;
  background-color: #fffbe6;
  border: 1px solid #ffa500;
  border-radius: 8px;
  padding: 20px 0 10px 0;
}

.empty {
  text-align: center;
}

.order-title {
  font-weight: bold;
  color: #ffa500;
  margin-left: 30px;
  margin-bottom: 10px;
}
.order-goods-list {
  width: 100%;
  background: #fff;
  display: flex;
  flex-wrap: wrap;
  padding: 10px 0 10px 0;
}
.order-goods {
  display: flex;
  align-items: center;
  width: 100%;
  border-bottom: 1px solid #ffa500;
  padding: 10px 0;
}
.order-goods-img {
  width: 70px;
  height: 70px;
  margin-left: 70px;
}
.order-goods-intro {
  width: 350px;
  margin: 0 0 0 15px;
  font-size: 13px;
  word-break: break-all;
  white-space: normal;
  overflow-wrap: break-word;
}
.order-goods-price {
  width: 100px;
  margin-left: 28px;
  text-align: center;
  font-size: 15px;
}
.order-goods-num {
  margin-left: 90px;
  font-size: 13px;
  color: #666;
}
.order-total {
  text-align: right;
  margin-right: 50px;
  color: #ffa500;
  font-weight: bold;
  margin-top: 10px;
}
</style>
